<?php

namespace app\course\event\listeners\course;

use app\common\event\subscribes\notify\Notify as NotifyEvent;
use app\school\logic\school\School;
use app\user\logic\teacher\Teacher;
use think\facade\Event;

/**
 * 课时审核消息通知事件监听类
 */
class ExamineNotify
{
    public static function handle($data)
    {

        /*
        平台讲师：
            讲师开启审核，只发消息给平台（默认机构）审核，审核结果通过或不通过，发消息给讲师
            讲师不开启审核，不发消息，直接通过
        非平台讲师：
        1、开启讲师审核，不开启机构审核
            机构审核，发消息给机构，机构审核通过或不通过，发消息给讲师
        2、开启讲师审核，开启机构审核
            前端：机构审核，发消息给机构，平台审核，发消息给平台
                a、机构审核通过，发消息给平台，平台审核；平台审核通过，发消息给讲师，不通过，发消息给机构
                b、机构审核不通过，发消息给讲师
        3、不开启讲师审核，不开启机构审核
            不审核，不发消息给平台或机构，直接通过
        4、不开启讲师审核，开启机构审核
            平台审核，发消息给平台，审核结果发消息给讲师
        */

        $param = static::getParam($data);
        // file_put_contents('examine_param.txt', json_encode( array_merge($data, $param) ) );
        if (count($param)) {
            Event::subscribe(NotifyEvent::class);
            Event::trigger('Notify', $param);
        }
    }

    public static function getParam($data): array
    {

        switch ($data['type']) {
            case 'submit':
                return static::getSubmitParam($data['data']);

            case 'examine':
                return static::getExamineParam($data['data']);

            default:
                return [];
        }
    }

    // 讲师提交课时审核
    public static function getSubmitParam($data): array
    {

        if (1 == $data['status']) return [];

        return [
            'user' => $data['admin_user_id'],
            'type' => 'system_message',
            'key'  => 'teacher_section_auth',
            'var'  => [
                'teacher_name' => $data['sec_teacher'],
                'course_title' => $data['sec_title']
            ]
        ];
    }

    // 后台审核结果
    public static function getExamineParam($data): array
    {
        $status             = $data['status'] ?? null;
        $reason             = $data['reason'] ?? '';
        $sec_update_teacher = $data['sec_update_teacher'] ?? 0;
        $sec_update_admin   = $data['sec_update_admin'] ?? 0;
        $sec_title          = $data['sec_title'] ?? '';
        $admin_mhm_id       = $data['admin_mhm_id'] ?? 0;

        if (3 == $status) {
            // 机构审核通过，交由平台审核
            return [
                'user' => 1,
                'type' => 'system_message',
                'key'  => 'school_section_auth',
                'var'  => [
                    'course_title' => $sec_title,
                    'school_title' => app(School::class)->getSchoolTitle($admin_mhm_id)
                ]
            ];
        } else if (in_array($status, [0, 1])) {

            if ($admin_mhm_id == 1) {
                // 平台审核，3-0、1
                // 判断最后修改人
                $key1 = $sec_update_teacher ? 'teacher_section_' : 'school_section_';
                $key2 = $status === 1 ? 'pass' : 'reject';
                $key  = $key1 . $key2;
                $user = $sec_update_teacher ?: $sec_update_admin;
                $type = $sec_update_teacher ? 'system_notify' : 'system_message';
            } else {
                // 机构审核，2-0、1（ps:讲师一定是开启的，且不可能是机构创建）
                $user = $sec_update_teacher;
                $key  = $status === 1 ? 'teacher_section_pass' : 'teacher_section_reject';
                $type = 'system_notify';
            }

            return [
                'user' => $user,
                'type' => $type,
                'key'  => $key,
                'var'  => [
                    'user_name'    => app(Teacher::class)->getTeacherName($user),
                    'course_title' => $sec_title,
                    'reason'       => $reason
                ]
            ];
        } else {
            return [];
        }
    }

}
